package com.moodswings.server.service;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class DbSession {

	public static final String url = "jdbc:mysql://localhost:3306/";

	public static final String dbName = "moodswings";
	public static final String driver = "com.mysql.jdbc.Driver";
	public static final String userName = "root";
	public static final String password = "987987";

	protected Connection conn = null;
	protected PreparedStatement pstmt;

	public static void init() {
		try {
			Class.forName(driver);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}

	public void connect() {
		try {
			conn = DriverManager.getConnection(url + dbName, userName, password);
			System.out.println("Connected");
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public void close() {
		try {
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public PreparedStatement createStatement(String sql) {
		try {
			return conn.prepareStatement(sql);
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}

}